package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Attention;
import com.pojo.R;
import com.service.AttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class AttentionConroller {
    @Autowired
    private AttentionService attentionService;
    @RequestMapping("/selectAllAttention")
    public Object selectAllAttention(@RequestParam(defaultValue = "1")String pageNum,long userId){
        PageInfo<Attention> attentionPageInfo = attentionService.selectAll(Integer.parseInt(pageNum), userId);
        return attentionPageInfo;
    }
    @RequestMapping("/userAttenjian")
    public Object AttentionNo(long userId,long projectId){
        int i = attentionService.deleteAtten(userId, projectId);
        R r= new R();
        if (i==1){
            r= new R("取消关注成功","200",null);
        }else {
            r= new R("取消关注失败","400",null);
        }
        return r;
    }
    /**
     * 添加关注
     * @param userId
     * @param projectId
     * @return
     */
    @RequestMapping("/userAttenjia")
    public Object Attentionadd(long userId,long projectId){
        int i = attentionService.addAtten(userId, projectId);
        R r= new R();
        if (i==1){
            r= new R("关注成功","200",null);
        }else {
            r= new R("关注失败","400",null);
        }
        return r;
    }

    /**
     * 关注前后进行判断，是否已经关注
     * @param userId
     * @param projectId
     * @return
     */
    @RequestMapping("/checkAtten")
    public Object checkAtten(long userId,long projectId){
        Attention attention = attentionService.selectAtten(userId, projectId);
        R r= new R();
        if (attention!=null){
            r= new R("已关注","200",null);
        }else {
            r= new R("未关注","400",null);
        }
        return r;
    }
}
